package com.sh4.wxservice;

import java.util.List;

import com.sh4.util.HttpUtil;
import com.sh4.wxservice.message.resp.Article;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 客服中心 Created by taotao on 2017/7/7.
 */
public class ClientService {

	private static String MSUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
	
	private static String MSTemplateMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

	public static void main(String[] args) {
		// ClientService.SNewsMsg("oFmnhv9GOIVNXk5gKF6ZQbBU7Gjg", "聚划算红包",
		// "尊敬的胡晓航,恭喜您获得一张分享红包", "", "",
		// "10_jR9UvpcTZJZseh6vG0Ao2ZvusDVD6e5mmA9_J3myz6r90aBxRkD7zoXzd-TEBl7PlDdH8jXwOPp6Zbz_hFDeIulUGbQF3mftvFyRWw8kC1BwFGZvvt6BmpR8qlPKl8Uh-1ubqKEIQNTAy_8LCELjAGAVXE");
		ClientService.SNewsMsg("oFmnhv9GOIVNXk5gKF6ZQbBU7Gjg", "聚划算红包到账通知", "尊敬的胡晓航 \n  恭喜您获得一张聚划算分享红包，点击查看详情",
				"http://weixin3.kmtsolution.com/KmtMember360/mobile/share/server?state=JIUDONG&url=http://kmtmember360.pazishizhuang.com/weixin/dist/&path=/member-center",
				"",
				"10_jR9UvpcTZJZseh6vG0Ao2ZvusDVD6e5mmA9_J3myz6r90aBxRkD7zoXzd-TEBl7PlDdH8jXwOPp6Zbz_hFDeIulUGbQF3mftvFyRWw8kC1BwFGZvvt6BmpR8qlPKl8Uh-1ubqKEIQNTAy_8LCELjAGAVXE");

	}

	/**********
	 * 客服接口- 发消息
	 *
	 *******/

	/**
	 * 发送模板消息 新订单待处理通知
	 * 
	 * @return
	 * @throws Exception
	 */
	public static JSONObject MSTemplate(String openId, String nicknsame ,String orderDate,String address,String remark, String accessToken) {
		
		String json  = "{\n" + "\"touser\":\"%s\",\n"
				+ "\"template_id\":\"eEyGNBQBqQxUyIXsbGoe9fjLPwRg5P0fkUgBC3E77kQ\",\n" + "\"url\":\"\",\n"
				+ "\"topcolor\":\"#FF0000\",\n" + "\"data\":{\n" + "\"first\": {\n"
				+ "\"value\":\"新赣防水，您有新的预约订单如下：\",\n" + "\"color\":\"#173177\"\n" + "},\n" + "\"keynote1\":{\n"
				+ "\"value\":\"%s\",\n" + "\"color\":\"#173177\"\n" + "},\n" + "\"keynote2\":{\n"
				+ "\"value\":\"%s\",\n" + "\"color\":\"#173177\"\n" + "},\n" + "\"keynote3\":{\n"
				+ "\"value\":\"%s\",\n" + "\"color\":\"#173177\"\n" + "},\n" + "\"remark\":{\n"
				+ "\"value\":\"%s\",\n" + "\"color\":\"#173177\"\n" + "}\n" + "}\n" + "}";
		JSONObject rs = null;
		try {
			
			String sb = String.format(json, openId ,nicknsame ,orderDate ,address ,remark);
			
			System.out.println("模板消息内容content.toString() = " + sb);
			
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSTemplateMsgUrl + accessToken));
		} catch (Exception e) {

		}
		return rs;
	}

	/**
	 * 发送text消息
	 * 
	 * @return
	 * @throws Exception
	 */
	public static boolean MSText(String openId, String content, String accessToken) {
		String json = "{\"touser\":\"%s\",\"msgtype\":\"text\",\"text\":{\"content\":\"%s\"}}";
		String sb = String.format(json, openId, content);
		Object rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {

		}
		return "0".equals(rs.toString());
	}

	// 发送图文消息（点击跳转到外链） 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
	/**
	 * 发送多条自定义图文消息
	 * 
	 * @param openId
	 * @param articles
	 *            List<Article>
	 * @return
	 * @throws Exception
	 */
	public static Boolean SNewsMsgs(String openId, List<Article> articles, String accessToken) {
		if (articles.isEmpty()) {
			return false;
		}
		if (articles.size() > 8) {
			return false;
		}
		String json = "{\"touser\":\"%s\",\"msgtype\":\"news\", \"news\":{\"articles\":%s}}";
		String sb = String.format(json, openId, JSONArray.fromObject(articles));
		JSONObject rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken));
			if (rs.getString("errcode").equals("0")) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送 单条 自定义图文消息
	 * 
	 * @param openId
	 *            会员openId
	 * @param title
	 *            图文消息 标题
	 * @param desc
	 *            图文消息描述
	 * @param url
	 *            图文消息跳转连接
	 * @param picUrl
	 *            消息图片
	 * @return
	 */
	public static Boolean SNewsMsg(String openId, String title, String desc, String url, String picUrl,
			String accessToken) {

		String postJson = "{ \"touser\":\"%s\", \"msgtype\":\"news\", \"news\":{ \"articles\": [ { \"title\":\"%s\", \"description\":\"%s\", \"url\":\"%s\", \"picurl\":\"%s\"} ] }}";
		String outputStr = String.format(postJson, openId, title, desc, url, picUrl);
		Object rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(outputStr, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送服务号 内部图文消息
	 * 
	 * @param openId
	 * @param media_id
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID 是
	 * @return
	 * @throws Exception
	 */
	public static boolean MsgNews(String openId, String media_id, String accessToken) throws Exception {
		String json = "{ \"touser\":\"%s\",\"msgtype\":\"mpnews\",\"mpnews\":{\"media_id\":\"%s\"}}";
		String sb = String.format(json, openId, media_id);
		Object rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送 内部图片消息
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Boolean MSImage(String openId, String media_id, String accessToken) {
		String json = "{\"touser\":\"%s\",\"msgtype\":\"image\",\"image\":{ \"media_id\":\"%s\"}}";

		Object rs = null;
		try {
			String sb = String.format(json, openId, media_id);
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送 内部语音消息
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Boolean MSVoice(String openId, String media_id, String accessToken) {
		String json = "{\"touser\":\"%s\",\"msgtype\":\"voice\",\"voice\": { \"media_id\":\"%s\"}}";
		Object rs = null;
		try {
			String sb = String.format(json, openId, media_id);
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送 内部视频消息
	 * 
	 * @param openId
	 * @param media_id
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID 是
	 * @param thumb_media_id
	 *            缩略图的媒体ID 是
	 * @param title
	 *            图文消息/视频消息/音乐消息的标题 否
	 * @param description
	 *            图文消息/视频消息/音乐消息的描述 否
	 * @return
	 * @throws Exception
	 */
	public static Boolean MSVideo(String openId, String media_id, String thumb_media_id, String title,
			String description, String accessToken) throws Exception {
		String json = "{\"touser\":\"%s\", \"msgtype\":\"video\",\"video\": {\"media_id\":\"%s\",\"thumb_media_id\":\"%s\",\"title\":\"%s\",\"description\":\"%s\"}}";
		Object rs = null;
		try {
			String sb = String.format(json, openId, media_id, thumb_media_id, title, description);
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());

	}

	/**
	 * 发送 内部音乐消息
	 * 
	 * @param openId
	 *            普通用户openid
	 * @param title
	 *            图文消息/视频消息/音乐消息的标题 否
	 * @param description
	 *            的描述 否
	 * @param musicurl
	 *            音乐链接 是
	 * @param hqmusicurl
	 *            高品质音乐链接，wifi环境优先使用该链接播放音乐 是
	 * @param thumb_media_id
	 *            缩略图的媒体ID 是
	 * @return
	 * @throws Exception
	 */
	public static Boolean MSMusic(String openId, String title, String description, String musicurl, String hqmusicurl,
			String thumb_media_id, String accessToken) {
		String json = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":{\"title\":\"%s\",\"description\":\"%s\",\"musicurl\":\"%s\",\"hqmusicurl\":\"%s\", \"thumb_media_id\":\"%s\" }}";
		String sb = String.format(json, openId, title, description, musicurl, hqmusicurl, thumb_media_id);
		Object rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/**
	 * 发送卡券
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Boolean MSCard(String openId, String cardId, String accessToken) throws Exception {
		String json = "{\"touser\":\"%s\", \"msgtype\":\"wxcard\",\"wxcard\":{\"card_id\":\"%s\"},}";
		String sb = String.format(json, openId, JSONArray.fromObject(cardId));

		Object rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doPost(sb, MSUrl + accessToken)).get("errcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0".equals(rs.toString());
	}

	/*************************
	 *
	 * 客服管理
	 *
	 ***************/

	/**
	 * 获取所有客服账号
	 *
	 * @return
	 * @throws Exception
	 */
	public static JSONObject getKfList(String accessToken) {
		String url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + accessToken;
		JSONObject rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doGet(url));
		} catch (Exception e) {

		}
		return rs;
	}

	/**
	 * 获取客服 状态 基本信息 @gh_d419400fcef0
	 *
	 * @return
	 * @throws Exception
	 */
	public static JSONObject getOnlineKfList(String accessToken) {
		String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" + accessToken;
		JSONObject rs = null;
		try {
			rs = JSONObject.fromObject(HttpUtil.doGet(url));
		} catch (Exception e) {

		}
		return rs;
	}

}